<?php
class RoleController extends BaseController
{
	public function index()
	{
		$this->view('all');
	}

	public function view($name = null)
	{
		$this->registry->template->setMenuKey('role');
		$tem = $this->registry->template;
		if ($name == 'all') {
			$tags['manuContentTitle'] = 'Role Management';
			$content = $this->getRoleTable();
		}
		else {
			$tags['manuContentTitle'] = 'View Role: ' . $name;
			$atts = array('ROLE', 'PASSWORD_REQUIRED');
			$content = $this->getGeneralTable($name, $atts, 'role') . '</br>' .
			$this->getSysPrivTbl($name) . '</br>' .
			$this->getObjPrivTbl($name) . '</br>' .
			$this->getUserTbl($name);
		}

		$tags['path'] = MANU_ROOT_PATH . 'role';
		$tags['action'] = "edited";
		$tags['what'] = "role";

		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);
				

			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}

		$output = $this->registry->template->getPageContent('index', 'Oracle User Manager', $content, $tags);

		echo $output;
	}

	public function grantRole($name = null)
	{
		$this->registry->template->setMenuKey('role');
		$tem = $this->registry->template;
		if ($name == "") {
			$tags['manuContentTitle'] = 'Grant Role';
			$content = $this->getRoleTableOfMine();
		}
		else {
			$tags['manuContentTitle'] = 'View Role: ' . $name;
			$atts = array('ROLE', 'PASSWORD_REQUIRED');
			$content = $this->getGeneralTable($name, $atts, 'role') . '</br>' .
			$this->getSysPrivTbl($name) . '</br>' .
			$this->getObjPrivTbl($name) . '</br>' .
			$this->getUserTbl($name);
		}
		$tags['path'] = MANU_ROOT_PATH . 'role';
		$tags['what'] = 'role';
		if (isset($_SESSION['message'])) {
			if ($_SESSION['message'] == 'addsuccess') {
				$tags['action'] = "add";
				$tags['manuMessage'] = ($_SESSION['message'] == 'addsuccess') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);
			}
			else if ($_SESSION['message'] == 'editsuccess') {
				$tags['action'] = "edit";
				$tags['manuMessage'] = ($_SESSION['message'] == 'editsuccess') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);
			}
			else if ($_SESSION['message'] == 'grantsuccess') {
				$tags['action'] = "grant/revok";
				$tags['manuMessage'] = ($_SESSION['message'] == 'grantsuccess') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);
			}
				
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}

		$output = $this->registry->template->getPageContent('index', 'Oracle User Manager', $content, $tags);

		echo $output;
	}
	public function getRoleTable()
	{
		//some basic info showed
		//TODO: Need to modify
		$atts = array('ROLE', 'PASSWORD_REQUIRED');
			
		$rows = $this->registry->db->get('roleManager')->getBasicInfo();

		$res = $this->getEntityTable($atts, $rows, 'role');

		return $res;
	}
	public function getRoleTableOfMine()
	{
		//some basic info showed
		//TODO: Need to modify

		$userMan = $this->registry->db->get('userManager');
		$user = $userMan->getCurrentUser();
		$user = strtoupper($user);

		$man = $this->registry->db->get('privilegeManager');
		$dba = $man->getDBA($user);

		if(count($dba) != 0 )
		$atts = array('GRANTED_ROLE');
		else
		$atts = array('GRANTED_ROLE', 'ADMIN_OPTION');

		$rows = $this->registry->db->get('roleManager')->getRoleOfMine($user);

		$res = $this->getEntityTable($atts, $rows, 'role', /*grant*/true);

		return $res;
	}
	public function getSysPrivTbl($name)
	{
		$man = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;

		$atts = array('PRIVILEGE', 'ADMIN_OPTION');
		$rows = $man->getSysPrivOf($name);

		$res = '<div id="title">System Privileges</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	public function getObjPrivTbl($name)
	{
		$man = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;

		$atts = array('PRIVILEGE', 'TABLE_NAME', 'COLUMN_NAME', 'GRANTABLE');
		$rows = $man->getObjPrivOf($name);

		$res = '<div id="title">Object Privileges</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	/*
	 * @description:	Get users who are granted the role
	*/
	public function getUserTbl($name)
	{
		$man = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;

		$atts = array('GRANTEE', 'ADMIN_OPTION');
		$rows = $man->getUserOf($name);

		$res = '<div id="title">Users Have The Role</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	//@author tubaduc
	public function edit ($nameRole) {
		$method = $_SERVER['REQUEST_METHOD'];
		$userMan = $this->registry->db->get('userManager');
		$roleMan = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;
		$tags['manuContentTitle'] = 'Edit Role';
		$tags['role_val'] = $nameRole;
		$tags['userOrRole'] = "";
		if($method == 'POST') {
			if(isset($_POST["newpassword"])) {
				$newpassword = strip_tags($_POST["newpassword"]);
				if ($newpassword == "") {
					//header("Location: ../add/username");
					//exit();
				}
			}
			if(isset($_POST["password"])) {
				$password = strip_tags($_POST["password"]);
				if ($password == "") {
					//header("Location: ../add/username");
					//exit();
				}
			}
				
			$roleMan->editRole($nameRole, $password, $newpassword);
			header("Location: ../index");
			exit();
		}
		else {
			$res = $roleMan->getBasicInfo($nameRole);
			foreach ($res as $re) {
				if($re->get('PASSWORD_REQUIRED') == 'NO'){
					$tags['nopass']="selected";
					$tags['yespass']="";
					$tags['passdisabled']="disabled";
				}
				else {
					$tags['nopass']="";
					$tags['yespass']="selected";
					$tags['passdisabled']="";
				}
			}
			$content = $tem->getModule('editrole', $tags);
		}
		if (isset($_SESSION['message'])) {
			$tags['manuMessage'] = ($_SESSION['message'] == 'success') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);

		echo $output;
	}

	public function editGrant ($nameRole) {
		$method = $_SERVER['REQUEST_METHOD'];
		$userMan = $this->registry->db->get('userManager');
		$roleMan = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;
		$tags['manuContentTitle'] = 'Grant/Revoke Role';
		$tags['role_val'] = $nameRole;
		$tags['userOrRole'] = "";
		$tags['alluser'] = "";
		$tags['grantee'] = "";
		if($method == 'POST') {
			if(isset($_POST["granttorole"])) {
				$granttorole = strip_tags($_POST["granttorole"]);
			}
			if(isset($_POST["granttouser"])) {
				$granttouser = strip_tags($_POST["granttouser"]);
			}
			$adminoption = null;
			if(isset($_POST["adminoption"])) {
				$adminoption = strip_tags($_POST["adminoption"]);
			}
			if(isset($_POST["revokeuser"])) {
				$revoke = strip_tags($_POST["revokeuser"]);
			}
			$roleMan->editGrantRole($nameRole, $granttorole, $granttouser, $revoke,$adminoption );
			header("Location: ../mine");
			exit();
		}
		else {
			$res2 = $roleMan->getBasicInfo();
			foreach ($res2 as $re2) {
				$tags['userOrRole'] .= "<option value=".$re2->get('ROLE').">".$re2->get('ROLE')."</option>";
			}
			$res3 = $userMan->getBasicInfo();
			foreach ($res3 as $re3) {
				$tags['alluser'] .= "<option value=".$re3->get('USERNAME').">".$re3->get('USERNAME')."</option>";
			}
			$res4 = $roleMan->getUserOf($nameRole);
			foreach ($res4 as $re4) {
				$tags['grantee'] .= "<option value=".$re4->get('GRANTEE').">".$re4->get('GRANTEE')."</option>";
			}
			$content = $tem->getModule('editGrantRole', $tags);
		}

		if (isset($_SESSION['message'])) {
			$tags['manuMessage'] = ($_SESSION['message'] == 'success') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}

		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);

		echo $output;
	}
	public function add () {
		$this->registry->template->setMenuKey('role');
		$this->registry->template->setMenuKey('role');
		$method = $_SERVER['REQUEST_METHOD'];
		$userMan = $this->registry->db->get('userManager');
		$roleMan = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;
		$tags['manuContentTitle'] = 'Add Role';

		if($method == 'POST') {
			if(isset($_POST["rolename"])) {
				$rolename = strip_tags($_POST["rolename"]);
				if ($rolename == "") {
					//header("Location: ../add/username");
					//exit();
				}
			}
			if(isset($_POST["password"])) {
				$password = strip_tags($_POST["password"]);
				if ($password == "") {
					//header("Location: ../add/username");
					//exit();
				}
			}
			$user = $userMan->getCurrentUser();
			$pass = $userMan->getPassOfCurrentUser();
			$roleMan->addRole($user, $pass, $rolename, $password);
			header("Location: ../role/mine");
			exit();
		}
		else {
			$create = $roleMan->checkCreateRole();
			if($create == false){
				$content = <<<EOT
User don't have CREATE ROLE Privilege !<br /><br />
EOT;
			}else
			$content = $tem->getModule('addrole', $tags);
		}

		if (isset($_SESSION['message'])) {
			$tags['manuMessage'] = ($_SESSION['message'] == 'success') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}

		if (isset($_SESSION['message'])) {
			$tags['manuMessage'] = ($_SESSION['message'] == 'success') ? $tem->getModule('sucmess', $tags) : $tem->getModule('errmess', $tags);
			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}

		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);

		echo $output;
	}

	/*
	 * @description:	Delete role
	*/
	public function delete($role)
	{
		$man = $this->registry->db->get('roleManager');
		$tem = $this->registry->template;

		$man->deleteRole($role);

		header("Location: ../index");
			
		exit();
	}
}
?>